﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;
using NHibernate;

namespace Com.CMS263.DAO
{
    public class CommunityDAO : BaseService
    {
        /// <summary>
        /// 根据sql查询集合结果
        /// </summary>
        /// <param name="hql"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<Community> GetListBySql(string hql, int page, int pagesize)
        {
            ISession session = DBHelper.GetISession;
            try
            {
                if (page == -1)
                {
                    return session.CreateQuery(hql).List<Community>();
                }
                else
                {
                    return session.CreateQuery(hql).SetFirstResult((page - 1) * pagesize).SetMaxResults(pagesize).List<Community>();
                }
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 根据sql查询条目数
        /// </summary>
        /// <param name="hql"></param>
        /// <returns></returns>
        public int GetCountBySql(string hql)
        {
            ISession session = DBHelper.GetISession;
            try
            {
                return session.CreateQuery(hql).List<Community>().Count;
            }
            catch
            {
                return 0;
            }
        }
        public IList<Community> GetListBySortId(string key, int page, int pagesize, string sort, string area, string price)
        {
            string hql = " from Community where 1=1 ";
            if (!string.IsNullOrEmpty(key))
            {
                hql += " and Name like '%" + key + "%'";
            }
            if (!string.IsNullOrEmpty(area))
            {
                hql += " and Regionarea like '" + area + "'";
            }
            if (!string.IsNullOrEmpty(price))
            {
                hql = GetPriceHql(hql, price);
            }
            if (!string.IsNullOrEmpty(sort))
            {

                if (sort == "1")
                {
                    hql += " order by Price asc";
                }
                else if (sort == "2")
                {
                    hql += " order by Price Desc";
                }
                else if (sort == "3")
                {
                    hql += " order by TotalCount desc";
                }
                else
                {
                    hql += " order by id desc";
                }
            }

            return GetListBySql(hql, page, pagesize);
        }
        public int GetListBySortId(string key, string sort, string area, string price)
        {
            string hql = " from Community where 1=1 ";
            if (!string.IsNullOrEmpty(key))
            {
                hql += " and Name like '%" + key + "%'";
            }
            if (!string.IsNullOrEmpty(area))
            {
                hql += " and Regionarea like '" + area + "'";
            }
            if (!string.IsNullOrEmpty(price))
            {
                hql = GetPriceHql(hql, price);
            }
            return GetCountBySql(hql);
        }
        public string GetPriceHql(string hql, string price)
        {
            if (price == "1万以下")
            {
                hql += " and Price <10000";
            }
            else if (price == "1-1.5万")
            {
                hql += " and Price >=10000 and Price <=15000";
            }
            else if (price == "1.5-2万")
            {
                hql += " and Price >=15000 and Price <=20000";
            }
            else if (price == "2-2.5万")
            {
                hql += " and Price >=20000 and Price <=25000";
            }
            else if (price == "2.5-3万")
            {
                hql += " and Price >=25000 and Price <=30000";
            }
            else if (price == "3-5万")
            {
                hql += " and Price >=30000 and Price <=50000";
            }
            else if (price == "5万以上")
            {
                hql += " and Price >50000";
            }
            return hql;
        }
    }
}
